import React, { PureComponent } from 'react';
import { connect } from 'dva';
import 'moment/locale/zh-cn';
import { TableC } from '../../../comps';
@connect(({ roleModel, userM: user, loading }) => ({
  roleModel,
  user,
  loading: loading.effects['roleModel/Search'],
}))
export default class TableForm extends PureComponent {
  state = {};

  showCreatedBy = (tenantData, val) => {
    if (!tenantData) return '';
    let text = '';
    tenantData.forEach((item) => {
      if (item.value === val) text = item.title;
    });
    return text;
  };
  //表格页码排序改变调用
  handleTableChange = (pagination) => {
    const { current, pageSize } = pagination;
    this.props.changeReset(current, pageSize);
  };

  render() {
    let that = this;
    const { data, onEditFrom, onDeleteFrom } = this.props;
    const {
      roleModel: { tenantData },
    } = this.props;
    let superMan = localStorage.getItem('antd-pro-superMan');
    let columns = [];
    if (superMan == 'true') {
      columns = [
        {
          title: '租户名称',
          dataIndex: 'createdBy',
          key: 'createdBy',
          render: (val) => <span>{that.showCreatedBy(tenantData, val)}</span>,
        },
        {
          title: '角色代码',
          dataIndex: 'roleId',
          key: 'roleId',
        },
        {
          title: '角色名称',
          dataIndex: 'roleName',
          key: 'roleName',
        },
        {
          title: '是否为公共角色',
          dataIndex: 'common',
          key: 'common',

          render: (val) => <span>{val == true ? '是' : '否'}</span>,
        },
        {
          title: '描述',
          dataIndex: 'description',
          key: 'description',
        },
        {
          title: '操作',
          dataIndex: 'operate',
          key: 'operate',
          type: 'operate',
          operates: [
            {
              title: '编辑',
              onClick: (record, index) => {
                onEditFrom(record);
              },
            },
            {
              title: '删除',
              onClick: (record, index) => {
                onDeleteFrom(record);
              },
            },
          ],
        },
      ];
    } else {
      columns = [
        {
          title: '角色名称',
          dataIndex: 'roleName',
          key: 'roleName',
        },
        {
          title: '角色代码',
          dataIndex: 'roleId',
          key: 'roleId',
        },
        {
          title: '是否为公共角色',
          dataIndex: 'common',
          key: 'common',
          render: (val) => <span>{val == true ? '是' : '否'}</span>,
        },
        {
          title: '描述',
          dataIndex: 'description',
          key: 'description',
        },
        {
          title: '操作',
          dataIndex: 'operate',
          key: 'operate',
          type: 'operate',
          operates: [
            {
              title: '编辑',
              onClick: (record, index) => {
                onEditFrom(record);
              },
              rules: {
                hide: { items: [{ key: 'common', value: [true], relation: ['='] }] },
              },
            },
            {
              title: '删除',
              onClick: (record, index) => {
                onDeleteFrom(record);
              },
              rules: {
                hide: { items: [{ key: 'common', value: [true], relation: ['='] }] },
              },
            },
          ],
        },
      ];
    }

    return (
      <TableC
        columns={columns}
        pagination={true}
        selfScrollY={true}
        dataSource={data}
        rowKey={'id'}
        loading={this.props.loading}
        current={this.props.currentPage}
        pageSize={this.props.numInPage}
        onChange={this.handleTableChange}
      />
    );
  }
}
